package caro.client.gui;
import javax.swing.*;
import java.awt.*;

import caro.client.gui.templates.AbstractWindow;
import caro.client.gui.templates.MyButton;
import caro.client.gui.templates.MyTextField;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.net.URL;

/**
 * Class Login
 * @author Thanh Nhan
 * @Since Aug 29, 2011
 * @Version 1.0
 */
public class Login extends AbstractWindow{
    private JTextField username = null;
    private JTextField password = null;
    
    private JButton login = null;
    private JButton cancel = null;
    
    private Socket socket = null;
    private ServerConnection serverConnectionGUI = null;
    
    public Login(Socket s,ServerConnection serverConnectionGUI){
        setTitle(Messages.getString("appTitle"));
        setResizable(false);
        socket =s;
        this.serverConnectionGUI = serverConnectionGUI;
        WIN_W = 400;
        WIN_H = 200;
        BORDER_H = 2;
        setSize(WIN_W, WIN_H);
        this.setLocationRelativeTo(null);
        setLayout(null);
        
        JLabel bgTitle = new JLabel(ImageProvider.getInstance().getImage("backgroundTitleShort.png"));
        bgTitle.setHorizontalAlignment(JLabel.LEFT);
        bgTitle.setLayout(null);
        
        JLabel title = new JLabel(Messages.getString("appTitle"));
        title.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 20));
        title.setForeground(new Color(19, 58, 10));
        title.setBounds((WIN_W - CELL_W*9)/2, 0, CELL_W*9, CELL_H);
        
        JLabel subTitle = new JLabel(Messages.getString("loginToServer", socket.getInetAddress().toString().substring(1)));
        subTitle.setFont(new Font("Arial", Font.BOLD, 13));
        subTitle.setForeground(new Color(19, 58, 10));
        subTitle.setBounds(WIN_W - CELL_W*11, BORDER_H + CELL_H, CELL_W*11, CELL_H);
        
        bgTitle.setBounds(0, 0, WIN_W, 50);
        bgTitle.add(title);
        bgTitle.add(subTitle);
        
        add(bgTitle);
        
        JLabel unernameLbl = new JLabel(Messages.getString("inputUsername"));
        unernameLbl.setBounds(BORDER_W, bgTitle.getHeight() + 8*BORDER_H, 6*CELL_W, CELL_H);
        URL imgUrl = getClass().getResource("images/bgTextField.png");
        JLabel passwordLbl = new JLabel(Messages.getString("inputPassword"));
        
        username = new MyTextField(imgUrl);
        username.setBorder(null);
        username.setBounds(BORDER_W + unernameLbl.getWidth() + BORDER_W, unernameLbl.getY(), 7*CELL_W, unernameLbl.getHeight());
        passwordLbl.setBounds(BORDER_W , unernameLbl.getY() + CELL_H + CELL_H/2, 6*CELL_W, unernameLbl.getHeight());
        password = new MyTextField(imgUrl);
        password.setBorder(null);
        password.setBounds(BORDER_W + passwordLbl.getWidth() + BORDER_W, passwordLbl.getY(), 7*CELL_W, passwordLbl.getHeight());
        add(unernameLbl);
        add(username);
        add(passwordLbl);
        add(password);
        
        JLabel bgButton = new JLabel(ImageProvider.getInstance().getImage("bgButton.png"));
        bgButton.setBounds(0, WIN_H - 75, WIN_W, 50);
        bgButton.setLayout(null);
        
        login = new MyButton(2*BORDER_W, 10*BORDER_H, 5*CELL_W, CELL_H, 
                ImageProvider.getInstance().getImage("button.png"), Messages.getString("login"));
        login.setFont(new Font("Arial", Font.BOLD, 13));
        cancel = new MyButton(2*BORDER_W + login.getWidth() + 2*BORDER_W, login.getY(), 5*CELL_W, CELL_H, 
                ImageProvider.getInstance().getImage("button.png"), Messages.getString("cancel"));
        cancel.setFont(new Font("Arial", Font.BOLD, 13));
        bgButton.add(login);
        bgButton.add(cancel);
        
        add(bgButton);
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username.getText();
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password.getText();
    }
    public Socket getSocket(){
        return socket;
    }
    
    public void addLoginListener(ActionListener a){
        login.addActionListener(a);
    }
    public void addCancelListener(ActionListener a){
        cancel.addActionListener(a);
    }
    @Override
    public ServerConnection getParent(){
        return serverConnectionGUI;
    }
}
